using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

/// <summary>
/// Summary description for ImageManager
/// </summary>
public static class ImageManager
{   
    public static void ResizeImage(Stream originalImageStream, string destinationFilePath, 
                                   string destinationFileName, ImageEncoding saveEncoding, int maxSideSize)
    {
        int intNewWidth;
        int intNewHeight;

        Bitmap original = new Bitmap(originalImageStream); // your original image
        
        //Set the image format
        ImageFormat fmtImageFormat = original.RawFormat;

        //Get the image original width and height
        int intOldWidth = original.Width;
        int intOldHeight = original.Height;

        //Determine if Landscape or Portrait
        int intMaxSide;

        if (intOldWidth >= intOldHeight)
        {
            intMaxSide = intOldWidth;
        }
        else
        {
            intMaxSide = intOldHeight;
        }


        if (intMaxSide > maxSideSize)
        {
            //set new width and height
            double dblCoef = maxSideSize / (double)intMaxSide;

            intNewWidth = Convert.ToInt32(dblCoef * intOldWidth);
            intNewHeight = Convert.ToInt32(dblCoef * intOldHeight);
        }
        else
        {
            intNewWidth = intOldWidth;
            intNewHeight = intOldHeight;
        }


        Size resolution = new Size(intNewWidth, intNewHeight); // size of your thumbnail
        
        Image thumbnail = new Bitmap(original, resolution);
        
        Graphics g = Graphics.FromImage(thumbnail);

        // The InterpolationMode was the catalyst to eliminate pixelation.
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.SmoothingMode = SmoothingMode.HighQuality;
        
        g.DrawImage(original, new Rectangle(0, 0, thumbnail.Size.Width, thumbnail.Size.Height));
        

        // Prepare for a controlled-quality JPEG export
        ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
        Encoder jpegEncoder = Encoder.Quality;
        EncoderParameters jpegEncoderParameters = new EncoderParameters(1);
        EncoderParameter jpegEncoderQuality = new EncoderParameter(jpegEncoder, 100L /*jpegQuality*/);
        
        jpegEncoderParameters.Param[0] = jpegEncoderQuality;

        thumbnail.Save(destinationFilePath + destinationFileName + ".jpg", jpegCodec, jpegEncoderParameters);
    }

    private static ImageCodecInfo GetEncoderInfo(string encoderString)
    {
        foreach (ImageCodecInfo info in ImageCodecInfo.GetImageEncoders())
        {
            if (info.MimeType == encoderString)
                return info;
        }

        return null;
    }



}


